﻿using System;
using System.Collections.Generic;
using System.Text;

namespace NielsRask.K242.Core
{
	public class Ship
	{
		public int BuildPointsMax = 0;
		public int BuildPoints = 0;
		public int HitPointsMax = 0;
		public int HitPoints = 0;
		public int FirePower = 0;
		public int Speed = 0;
		public ShipType Type { get; protected set; }
		public bool CanColonize = false;
		public float SensorRange = 0;


		public bool IsCompleted;

		public void Build(int points)
		{
			BuildPoints += points;
			if (BuildPoints >= BuildPointsMax)
			{
				BuildPoints = BuildPointsMax;
				HitPoints = HitPointsMax;
				IsCompleted = true;
			}
		}

		public delegate void DestroyedHandler(Ship sender);
		public event DestroyedHandler Destroyed;
		public void OnDestroyed()
		{
			if (Destroyed != null)
				Destroyed(this);
		}

		public void TakeDamage(int enemyFirePower)
		{
			HitPoints -= enemyFirePower;
			if (HitPoints <= 0)
				OnDestroyed();
		}
	}
	public enum ShipType
	{
		Scout,
		Corvette,
		Colony,
		Capital,
		Base
	}

	public class Scout : Ship
	{
		public Scout()
		{
			BuildPointsMax = 100;
			HitPointsMax = 20;
			FirePower = 5;
			Speed = 90;
			SensorRange = 100;
			Type = ShipType.Scout;
		}
	}

	public class Corvette : Ship
	{
		public Corvette()
		{
			BuildPointsMax = 500;
			HitPointsMax = 150;
			FirePower = 35;
			Speed = 65;
			SensorRange = 60;
			Type = ShipType.Corvette;
		}
	}
	public class ColonyShip : Ship
	{
		public ColonyShip()
		{
			BuildPointsMax = 2000;
			HitPointsMax = 1000;
			FirePower = 5;
			Speed = 35;
			Type = ShipType.Colony;
			SensorRange = 40;
			CanColonize = true;
		}
	}
	public class CapitalShip : Ship
	{
		public CapitalShip()
		{
			BuildPointsMax = 3000;
			HitPointsMax = 2000;
			FirePower = 200;
			Speed = 50;
			SensorRange = 75;
			Type = ShipType.Capital;
		}
	}
	public class SpaceBase : Ship
	{
		public SpaceBase()
		{
			BuildPointsMax = 5000;
			HitPointsMax = 5000;
			FirePower = 600;
			Speed = 0;
			SensorRange = 100;
			Type = ShipType.Base;
		}
	}

	public class ShipList : List<Ship>
	{
		public void Add(Ship ship)
		{
			ship.Destroyed += new Ship.DestroyedHandler(ship_Destroyed);
			base.Add(ship);
		}

		void ship_Destroyed(Ship sender)
		{
			this.Remove(sender);

			if (Count == 0)
				OnEmptied();
		}

		public delegate void EmptiedHandler(ShipList sender);
		public event EmptiedHandler Emptied;
		public void OnEmptied()
		{
			if (Emptied != null)
				Emptied(this);
		}

	}
}
